package com.cashmate.struts;

import com.cashmate.hibernate.dao.EmployeeDAO;
import com.cashmate.hibernate.dao.ProductDAO;
import com.cashmate.hibernate.pojo.Employees;
import com.cashmate.hibernate.pojo.Products;
import com.cashmate.hibernate.pojo.Usertypes;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

public class EditProducts extends ActionSupport {

    private static Logger logger = Logger.getLogger(EditProducts.class);
    private String oper;
    private String id;
    private int productname;
    private String productdesc;
    private final ProductDAO productdao = (ProductDAO) ServletActionContext.getServletContext().getAttribute("productDAO");
    private final EmployeeDAO empDao = (EmployeeDAO) ServletActionContext.getServletContext().getAttribute("employeeDAO");
    private Employees emps = (Employees) ServletActionContext.getRequest().getSession().getAttribute("employee");
    private Usertypes ut = (Usertypes) ServletActionContext.getRequest().getSession().getAttribute("usertype");

    @Action(value = "/edit-products", results = {
        @Result(name = "duplicate", location = "../error/formError.jsp"),
        @Result(name = "error", location = "../error/messages.jsp")
    })
    @Override
    public String execute() {
        try {
            Products prod = null;
            if (oper.equals("add")) {
                if (productdao.hasDuplicate(productname)) {
                    addActionError("ERROR : Product name " + productname + " already exists.");
                    return "duplicate";
                }
                prod = new Products(productname, productdesc, true);
                productdao.insert(prod);
                logger.info(ut.getType() + " (" + emps.getIdnumber() + ") added a new Product: {productid:" + id + ", productname:" + productname + ", productdesc:" + productdesc + "}");

            } else if (oper.equals("edit")) {
                prod = productdao.get(Integer.parseInt(id));
                if (prod.getProductname() != productname) {
                    if (productdao.hasDuplicate(productname)) {
                        addActionError("ERROR : Product name " + productname + " already exists.");
                        return "duplicate";
                    }
                }
                int oldprodname = prod.getProductname();
                prod.setProductname(productname);
                prod.setProductdesc(productdesc);
                prod.setIsactive(true);
                productdao.update(prod);
                logger.info(ut.getType() + " (" + emps.getIdnumber() + ") updated Product name(" + oldprodname + ") with new values: productid:" + id + ", productname:" + productname + ", productdesc:" + productdesc + "}");

            } else if (oper.equals("del")) {
                prod = productdao.get(Integer.parseInt(id));
                prod.setIsactive(false);
                productdao.update(prod);
                  logger.info(ut.getType() + " (" + emps.getIdnumber() + ") deleted Product name: " + prod.getProductname()+"}");

            }
        } catch (Exception e) {
            e.printStackTrace();;
            if (emps != null) {
                logger.fatal("(" + emps.getIdnumber() + "):" + e.getLocalizedMessage(), e);
            } else {
                logger.fatal(e.getLocalizedMessage(), e);
            }
            addActionError(e.getLocalizedMessage());
            return ERROR;
        }
        return NONE;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setProductdesc(String productdesc) {
        this.productdesc = productdesc;
    }

    public void setProductname(int productname) {
        this.productname = productname;
    }

    public void setOper(String oper) {
        this.oper = oper;
    }
}
